package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.EnterpriseDto;
import com.scpii.universal.hibernate.dao.EnterpriseManageDao;
import com.scpii.universal.service.EnterpriseManageService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-10下午05:21:48
 */
@Service
public class EnterpriseManageServiceImpl extends
		BaseServiceImpl<EnterpriseDto, Integer> implements EnterpriseManageService {
	@Resource
	private EnterpriseManageDao enterpriseManageDao;

	@Resource
	public void setBaseDao(EnterpriseManageDao enterpriseManageDao) {
		super.setBaseDao(enterpriseManageDao);
	}

	/**
	 * 根据分类ID获得下属分类
	 * 
	 * @param id
	 * @return
	 * @author zhangqiaing 2012-7-10上午11:05:44
	 */
	@Override
	public List<EnterpriseDto> getEnterpriceList(EnterpriseDto condition,Integer pageNo,Integer pageSize) {
		
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.EnterpriseDto(t1.intEnterpId,");
		hql.append(" t1.varEnterpName,");
		hql.append(" t1.intEnterpTradeType,");
		hql.append(" t1.varEnterpLegalPerson,");		
		hql.append(" t1.varEnterpLicenseImgUrl,");
		hql.append(" t1.varEnterpPhonenum,");
		hql.append(" t1.varEnterpEmail,");
		hql.append(" t1.varEnterpAddress,  ");
		hql.append(" t2.intTypeId , ");
		hql.append(" t2.varTypeName ) ");
		hql.append(" from EnterpriseTab t1 , ");
		hql.append("  TypeTab t2 ");
		hql.append(" where t1.intEnterpTradeType= t2.intTypeId ");			 
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		//查询条件
		if(condition!=null){
			if(condition.getIntEnterpId()!=null){
				hql.append(" and t1.intEnterpId=:intEnterpId ");
				argsMap.put("intEnterpId", condition.getIntEnterpId());
			}
			if(condition.getVarEnterpName()!=null&&!"".equals(condition.getVarEnterpName())){
				hql.append(" and t1.varEnterpName like:varEnterpName");
				argsMap.put("varEnterpName", condition.getVarEnterpName());
			}
			if(condition.getIntTypeId()!=null){
				hql.append(" and t2.intTypeId=:intTypeId");
				argsMap.put("intTypeId", condition.getIntTypeId());
			}
		}
		
		
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		return enterpriseManageDao.findByHql(hql.toString(), pageNo, pageSize, argsMap);
	}
	
	/**
	 * 根据id的信息，查找parentId等于当前id的数据有多少条记录
	 * @param id
	 * @return
	 * @author zhangqiaing
	 * 2012-7-10上午11:04:15
	 */
	@Override
	public Integer getCountById(EnterpriseDto condition) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.EnterpriseDto(t1.intEnterpId) ");		
		hql.append(" from EnterpriseTab t1 , ");
		hql.append("  TypeTab t2 ");
		hql.append(" where t1.intEnterpTradeType= t2.intTypeId ");				 
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		//查询条件
		if(condition!=null){
			if(condition.getIntEnterpId()!=null){
				hql.append(" and t1.intEnterpId=:intEnterpId ");
				argsMap.put("intEnterpId", condition.getIntEnterpId());
			}
			if(condition.getVarEnterpName()!=null&&"".equals(condition.getVarEnterpName())){
				hql.append(" and t1.varEnterpName like:varEnterpName");
				argsMap.put("varEnterpName", "%"+condition.getVarEnterpName()+"%");
			}
			if(condition.getIntTypeId()!=null){
				hql.append(" and t2.intTypeId=:intTypeId ");
				argsMap.put("intTypeId", condition.getIntTypeId());
			}
		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		List<EnterpriseDto> list=enterpriseManageDao.hqlGetList(hql.toString(), argsMap);
		if(list!=null){
			return list.size();
		}else{
			return 0;
		}
	}

	@Override
	public EnterpriseDto getEnterpriceById(Integer intEnterpId) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.EnterpriseDto(t1.intEnterpId,");
		hql.append(" t1.varEnterpName,");
		hql.append(" t1.intEnterpTradeType,");
		hql.append(" t1.varEnterpLegalPerson,");		
		hql.append(" t1.varEnterpLicenseImgUrl,");
		hql.append(" t1.varEnterpPhonenum,");
		hql.append(" t1.varEnterpEmail,");
		hql.append(" t1.varEnterpAddress,  ");
		hql.append(" t2.intTypeId , ");
		hql.append(" t2.varTypeName ) ");		
		hql.append(" from EnterpriseTab t1 , ");
		hql.append("  TypeTab t2 ");
		hql.append(" where t1.intEnterpTradeType= t2.intTypeId ");				 
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign");
		hql.append(" and t1.intEnterpId=:intEnterpId");
		
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		argsMap.put("intEnterpId", intEnterpId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<EnterpriseDto> list=enterpriseManageDao.hqlGetList(hql.toString(), argsMap);
		if(list!=null){
			return list.get(0);
		}else{
			return null;
		}		
	}
}
